home *** CD-ROM | disk | FTP | other *** search
/ PC Format (UK) 124 / pcfcd124-a.iso / Trial Software / BlitzBasic / Rift / tutorial1.bb < prev   
Encoding:
Text File  |  2001-04-23  |  12.0 KB  |  199 lines

  1. AppTitle "Celestial Rift - Version 0.5 (C)2001 Myke P" ;what this program will be called in MICROSOFT WINDOWS.
  2.  
  3. ;This makes two CONSTants. These are values which will NOT change at any point in the program, so we set their values now.
  4. Const SCREEN_WIDTH = 800
  5. Const SCREEN_HEIGHT = 600
  6. ;This is the code which tells Blitz what Display Resolution to use on the Graphics Card. The "GRAPHICS" command should always be placed before you do
  7. ;*anything* image-related in your program
  8. Graphics SCREEN_WIDTH,SCREEN_HEIGHT,0,2    ;start the graphics mode at SCREEN_WIDTH by SCREEN_HEIGHT, let Blitz choose the depth (,0) and run full screen (,1)
  9.  
  10. ;the following constants are keyboard "SCAN" codes. Every key on the keyboard has a number. You can get the full list in your Blitz manual.
  11. Const KEY_QUIT = 1            ;(Escape)
  12. Const KEY_SAVESCREEN = 88    ;(F12)
  13.  
  14. Global FLAG_MENUON
  15. Global FLAG_SAVESCREEN = 0
  16.  
  17. Global timer
  18. Global frames
  19. Global menu_frame
  20.  
  21. timer = CreateTimer(50) ;create a timer set at 50ms (game speed) - play with this to see how you can increase or decrease the speed of the game.
  22.                         ;this should be set at a speed which will look near enough the same on *every* PC it will be played on.
  23.                         ;My PC (a 733MHz PIII with an nVidia GeForce card) will handle upwards of 150 frames per second, quite happily
  24.                         ;but 'lesser' machines will not. 50, therefore, is quite sensible For a game of this nature, who's minimum system spec
  25.                         ;will be something like a PII 300MHz machine (i.e.: Blitz Basic's minimum spec!)
  26.  
  27. ;NOTE: there's no need to organise your variable declarations, as I have here, into sections. They can appear in any order you like, before the main program begins.
  28. ;I just do this, 'cos it looks right professional! :))))
  29.  
  30. ;picture (and related) variables
  31. Global menu_start
  32. Global menu_start_stat
  33. Global menu_logo
  34. Global menu_credit
  35. Global menu_guildhall
  36. Global menu_thanks
  37. Global menu_quit
  38. Global menu_ship1
  39. Global menu_ship2
  40. Global menu_ship_hor
  41. Global menu_ship_ver#
  42. Global menu_ship_type
  43.  
  44. ;these are all plain single-frame pictures
  45. ;menu piccies
  46. menu_logo = LoadImage("GfxRes/menu-logo.bmp")            ;the CELESTIAL RIFT logo
  47. menu_ship1 = LoadImage("GfxRes/menu-ship1.bmp")            ;the big player-type ship
  48. menu_ship2 = LoadImage("GfxRes/menu-ship2.bmp")            ;the big enemy type ship
  49. menu_guildhall = LoadImage("GfxRes/menu-guildhall.bmp")    ;the GUILDHALL message at the top
  50. menu_thanks = LoadImage("GfxRes/menu-thanks.bmp")        ;the THANKS message at the bottom
  51. menu_credit = LoadImage("GfxRes/menu-credit.bmp")        ;the CREDITS message below the logo
  52. menu_start = LoadImage("GfxRes/menu-start.bmp")            ;the "PRESS FIRE TO START" caption
  53. menu_quit = LoadImage("GfxRes/menu-quit.bmp")            ;the "(ESCAPE TO QUIT)" caption
  54. MaskImage menu_ship1,255,0,255            ;mask all the images.
  55. MaskImage menu_ship2,255,0,255            ;notice that it doesn't matter what order you do the masks in!
  56. MaskImage menu_credit,255,0,255
  57. MaskImage menu_guildhall,255,0,255        ;by the way, if when testing your program, you get the error message "IMAGE DOES NOT EXIST" at this point
  58. MaskImage menu_thanks,255,0,255            ;in the program, it's because the LOADIMAGE command above didn't find the file properly.
  59. MaskImage menu_start,255,0,255            ;This is usually either because; 1 - You've typed the wrong filename in the LOADIMAGE bit, or
  60. MaskImage menu_quit,255,0,255            ;2 - You've got the variable name wrong in the MASKIMAGE bit.
  61.                                         ;you could perform some checks at the end of loading all the images, so that you know they've all been loaded
  62.                                         ;successfully. If they haven't then you could end the program properly and tell the user that they've got
  63.                                         ;corrupt or missing files!
  64.  
  65.                                         ;as you'll see, I haven't bothered! :)
  66.  
  67. ;Right! That's it, we've set up *everything* we're going to need from outside the program.
  68. menu_loop() ;start the proper program loop by 'calling' the function called "menu_loop()" - which, conveniently, is just coming up!!!
  69.  
  70. ;this function keeps the menu loop going.. It starts playing tunes and sets a couple of variables. 
  71. ;Then it goes into a never ending loop which carries out a sequence of checks and function calls.
  72. Function menu_loop() ;Start of the Function whose name is "menu_loop()"
  73.     FLAG_MENUON = 1 ;tells the program that the menu is running, for use in the REPEAT..FOREVER statement in a sec..
  74.  
  75.     menu_ship_hor = (SCREEN_WIDTH/2)-(ImageWidth(menu_ship1)/2) ;this sets the starting horizontal position of the big ship on the menu
  76.     menu_ship_ver = SCREEN_HEIGHT+1    ;this sets the vertical starting position of the big ship on the menu at one pixel off the bottom of the screen.
  77.                                     ;notice that images are (by default) handled from the TOP-LEFT pixel, so, horizontally, I offset the value
  78.                                     ;to the left, by half the width of the ship image.
  79.     menu_ship_type = 1                ;set up some initial values for these counters
  80.     menu_frame = 1                    
  81.     menu_start_stat = 0
  82.     
  83.     music_menu_level = 1    ;this value will be used to set the volume of the music track in a minute..
  84.     Repeat        ;a REPEAT.. FOREVER loop will carry out the code in between for as long as the program is running..
  85.         If FLAG_MENUON = 1 Then    ;If the FLAG_MENUON flag is set to 1 then "do" the following..
  86.             menu_loop_update()    ;call function "menu_loop_update()"
  87.  
  88.         Else                                    ;if the FLAG_MENUON flag wasn't set to 1 then "do" the following..
  89.             End                                        ;END the program
  90.         End If
  91.     Forever    ;end of the REPEAT.. FOREVER loop
  92. End Function ;End of the function
  93.  
  94. ;"menu_loop_update()" checks for key-presses, alters position coordinates on the screen
  95. ;and various variables/flags for use in "menu_draw_update()" and the game functions too.
  96. ;in fact, you'll see that this has the same organisational structure as the proper game loop (albeit much less complex!)
  97. Function menu_loop_update()
  98.     frames = WaitTimer(timer) ;returns a value to "frames" for how many video screen refreshes (The MHz of your monitor) occured since the last call to our timer
  99.     For i = 1 To frames    ;update the screen positions for "frames" number of changes.. This enables drawing frames to be skipped on slower machines.
  100.         If KeyDown(KEY_QUIT)                ;If the user presses the "QUIT" button on the keyboard, then..
  101.             FlushKeys                        ;(when using SCAN CODES, flush the keyboard buffer after each successful "Have they pressed a particular key" question.)
  102.  
  103.                 FLAG_MENUON = 0                ;back in the "menu_loop()" function, this will cause the program to end!
  104.         End If
  105.  
  106.         If KeyDown(KEY_SAVESCREEN)            ;if the user presses the "SAVESCREEN" button on the keyboard, then..
  107.             FlushKeys
  108.             FLAG_SAVESCREEN = 1                    ;just sets FLAG_SAVESCREEN to 1. You'll see this used in the next function.
  109.         End If
  110.         If menu_ship_ver > (-50 - ImageHeight(menu_ship1)) ;if the bottom of the (largest) ship picture gets to 50 pixels above the top of the screen
  111.             Select menu_ship_type    ;A SELECT..CASE statement is like an IF statement, but only performs 1 check, i.e.: "What is the value of I?"
  112.                 Case 1    ;if "menu_ship_type" is 1 then..
  113.                     menu_ship_ver = menu_ship_ver - .3 ;how fast menuship1 moves up the screen
  114.                 Case 2    ;if "menu_ship_type" is 1 then..
  115.                     menu_ship_ver = menu_ship_ver - .8 ;how fast menuship2 moves up the screen
  116.             End Select
  117.         Else
  118.             menu_ship_ver = SCREEN_HEIGHT+1    ;reset the vertical position so that the top of the ship is at the bottom of the screen
  119.             menu_ship_hor = Rnd(0,SCREEN_WIDTH-ImageWidth(menu_ship1)) ;create a random horizontal position
  120.             menu_ship_type = Int(Rnd(1,2)) ;randomly choose between values 1 or 2
  121.         End If
  122.         menu_frame = menu_frame + 1        ;increase the value of "menu_frame" by 1
  123.         If menu_frame = 25 Then            ;if the value of "menu_frame" gets to 25 then reset it to 1
  124.             menu_frame = 1
  125.  
  126.             If menu_start_stat = 1 Then    ;this bit just switches the value of "menu_start_stat" between 1 and 0, i.e. :If it's 1, then make it 0, if it's 0, then make it 1 etc.
  127.                 menu_start_stat = 0        ;this will be used in the next function to flash the "PRESS FIRE TO START" caption on and off!
  128.             Else
  129.                 menu_start_stat = 1
  130.             End If
  131.         End If
  132.     Next
  133.     menu_draw_update() ;call the function "menu_draw_update" which draws all these new things on the screen
  134. End Function
  135.  
  136. ;"menu_draw_update()" just draws things on your monitor, using values set and altered in the last function (menu_loop_update).
  137. Function menu_draw_update()
  138.     SetBuffer BackBuffer()    ;draw all of the following to the backbuffer() which is an area video memory which is not shown on screen.
  139.                             ;the idea is that you draw everything here, then SHOW it to the user when the full picture is complete.
  140.                             
  141.     ClsColor 0,0,0        ;changes the CLearScreen colour to black (0,0,0)
  142.                         ;Color commands use the 3 parameters as RED value, GREEN value, BLUE value
  143.                         ;which is how pixel colours are made up on the monitor. If you're familiar with
  144.                         ;any PC Paint Packages, you'll probably be quite familiar with this.
  145.     Cls                    ;CLears the Screen
  146.     
  147.     ;this next section draws all of the pictures on to the backbuffer() at the specified coordinates.
  148.     ;notice that the pictures are drawn in sequence, with the backmost things drawn first and the foremost things drawn last
  149.     ;kind of like making a collage!
  150.     DrawImage menu_logo,(SCREEN_WIDTH/2)-(ImageWidth(menu_logo)/2),0    ;this draws the menu_logo image at "x","y"
  151.                         ;I've used EQUATIONS to make up x and y for all the images in this project, so that when you change
  152.                         ;the resolution values (back at the beginning of the code) everything still appears at the correct position
  153.                         ;on the screen. If you know what resolution you'll be working at, then you can quite happily put x and y as
  154.                         ;ordinary numbers in here.
  155.                         ;For example, at 800*600, the menu_logo image will be drawn at:
  156.                         ;SCREEN_WIDTH = 800
  157.                         ;800/2 = 400
  158.                         ;ImageWidth(menu_logo) is the width (in pixels) of this particular image (which is 547 pixels)
  159.                         ;547/2 = 273.5
  160.                         ;(800 - 273.5 = 126.5)
  161.                         ;So, menu_logo would be drawn at 127,0 (because Blitz requires whole number coordinates, it will automatically
  162.                         ;round up 126.5 to be 127.)
  163.     DrawImage menu_guildhall,(SCREEN_WIDTH/2)-(ImageWidth(menu_guildhall)/2),5
  164.     DrawImage menu_credit,(SCREEN_WIDTH/2)-(ImageWidth(menu_credit)/2),200
  165.     DrawImage menu_quit,(SCREEN_WIDTH/2)-(ImageWidth(menu_quit)/2),SCREEN_HEIGHT-75
  166.     DrawImage menu_thanks,(SCREEN_WIDTH/2)-(ImageWidth(menu_thanks)/2),SCREEN_HEIGHT-15
  167.  
  168.     ;depending on the value set earlier in "menu_loop_update()" this draws one of the two ships at the relevant coordinates on-screen
  169.     ;menu_ship_hor is the x position, and is set once per "scroll up the screen" in menu_loop_update().
  170.     ;menu_ship_ver is the y position, which is repeatedly changed in menu_loop_update() to scroll the ships up the screen!
  171.     Select menu_ship_type
  172.         Case 1
  173.             DrawImage menu_ship1,menu_ship_hor,menu_ship_ver
  174.         Case 2
  175.             DrawImage menu_ship2,menu_ship_hor,menu_ship_ver
  176.     End Select
  177.     
  178.     ;Remember before, I was saying that "menu_frame" would be used to flash the "START GAME" caption on and off?
  179.     ;when "menu_frame" got to 25, the variable "menu_start_stat" was flipped between 1 or 0
  180.     If menu_start_stat = 1 Then
  181.         ;if the variable "menu_start_stat" = 1 then draw the caption. If it isn't, ignore drawing the caption.
  182.         ;On and off, on and off. Simple as that! This technique will be used again later for the PAUSE and GAME_OVER captions!
  183.         DrawImage menu_start,(SCREEN_WIDTH/2)-(ImageWidth(menu_start)/2),(SCREEN_HEIGHT/2)+(ImageHeight(menu_start)*4)
  184.     End If
  185.  
  186.     ;Remember when we checked to see if the user had pressed the "SAVESCREEN" button, earlier?
  187.     ;if they did, we set FLAG_SAVESCREEN to 1. Now this is where that takes effect..
  188.     If FLAG_SAVESCREEN=1 Then
  189.         SaveBuffer (BackBuffer(),"CRMenuScreen.bmp")
  190.             ;the SaveBuffer command outputs the contents of the named buffer (in this case "BackBuffer()") to the named file, in a BMP format.
  191.         FLAG_SAVESCREEN = 0
  192.             ;after we've saved the screen, we reset the FLAG to 0, so that when we come back for the next "menu_draw_update", it won't save
  193.             ;another picture. (i.e.: it only saves 1 picture for each keypress of the "SAVESCREEN" button!)
  194.             ;This is useful for getting working screenshots of your project to impress your mates!! :)
  195.     End If 
  196.     Flip    ;finally, FLIP everything on the backbuffer() and show it on the frontbuffer(), i.e.: your monitor!!
  197. End Function
  198.  
  199.